// kendo ui widget configs
gridPageableConfig = {
	pageSize: 100,
	pageSizes: [50, 100, 200, 500],
	buttonCount: 20,
	input: false,
	info: true,
	messages: {
		display: "{0} - {1} of {2} records",
		empty: "Loading...",
		itemsPerPage: "records per page"
	}
}
gridFilterableConfig = {
	extra: false	
}
// kendo ui widget event functions
gridOnDataBound = function(grid) {
	var total = grid.dataSource.total();
	var hasFilters = $("#filters");
	$("#loading").hide();
	if (total > 0) {
		$("#noRecs").hide();
		if (hasFilters) {
			$("#filters").hide();
		}
		$("#countLabel").text(total + " records");
		$("#grid").show();
	} else {
		$("#grid").hide();
		$("#countLabel").text("0 records");
		if (hasFilters) {
			$("#filters").show();
		}
		$("#noRecs").show();
	}
	if ($(grid.pager).length > 0 && grid.dataSource.totalPages() <= 1) {
		grid.pager.element.hide();
	}
}
// shared ui functions
toggleFilter = function(filterId) {
	if ($("#" + filterId).is(":visible")) {
		$("#" + filterId).hide();
		$("#filterLink").text("Show filters");
	} else {
		$("#" + filterId).show();
		$("#filterLink").text("Hide filters");
	}
}
toggle = function(idToShow, idToHide) {
	$("#" + idToHide).hide();
	$("#" + idToShow).show();
}
// bootstrap ui functions
resizeModal = function(modalId) {
	var modalHeight = $("#" + modalId).height();
	var modalBodyHeight = modalHeight - 139;
	$("#" + modalId + " .modal-body").css("min-height", modalBodyHeight);
	$("#" + modalId + " .modal-body").css("max-height", modalBodyHeight);
	$("#" + modalId + " .modal-body").css("height", modalBodyHeight);
}
// data validation
isValidDate = function (value, userFormat) {
	var userFormat = userFormat || "m/d/yyyy"; // default format
	var delimiter = /[^mdy]/.exec(userFormat)[0];
	var theFormat = userFormat.split(delimiter);
	var theDate = value.split(delimiter);
	var isDate = function (date, format) {
		var m, d, y;
		for (var i=0, len=format.length; i<len; i++) {
			if (/m/.test(format[i])) m = date[i]
			if (/d/.test(format[i])) d = date[i]
			if (/y/.test(format[i])) y = date[i]
		}
		return (m > 0 && m < 13 && y && y.length === 4 && d > 0 && d <= (new Date(y, m, 0)).getDate())
	}
	return isDate(theDate, theFormat)
}
isInt = function(value) {
	var intRegex = /^\d+$/;
	return intRegex.test(value);
}
// models
getModel = function(obj) {
	switch(obj) {
		case "order":
			return {
				id: "order_id",
				fields: {
					order_id: {type: "number"},
					status: {type: "string"},
					order_date: {type: "date"},
					institution_type: {type: "string"},
					college_university: {type: "string"},
					high_school: {type: "string"},
					institution: {type: "string"},
					approved_date: {type: "date"},
					shipped_date: {type: "date"},
					count_ordered: {type: "number"},
					count_shipped: {type: "number"},
					quantity_ordered: {type: "number"},
					quantity_pending_shipment: {type: "number"},
					quantity_shipped: {type: "number"},
					wms_order_id: {type: "number"},
					next_workshop_date: {type: "date"}
				}	
			}
		break;
		case "product":
			return {
				id: "product_id",
				fields: {
					product_id: {type: "number"},
					id: {type: "string"},
					sku: {type: "string"},
					category: {type: "string"},
					product: {type: "string"},
					download_only: {type: "number"},
					inventory: {type: "number"},
					stock: {type: "number"},
					stock_warn: {type: "number"},
					allow_back_order: {type: "number"},
					package: {type: "number"},
					package_only: {type: "number"},
					quantity_ordered: {type: "number"},
					quantity_shipped: {type: "number"},
					active: {type: "number"}
				}
			}
		break;
		case "orderProduct":
		
		break;
		case "productCategory":
		
		break;
		case "user":
		
		break;
		case "tracking":
		
		break;
	}
}
getGridDataSourceConfig = function(obj) {
	return {
		transport: {
			read: {
				url: "/assets/csac/com/API.cfc", 
				dataType: "json", 
				type: "GET", 
				cache: false
			},
			parameterMap: function(params, transportType) {
				params.obj = obj;
				params.method = transportType;
				$.extend(params, filter);
				return params;
			}
		},
		schema: {model: getModel(obj)}
	}
}

